﻿using Bootstrap.Security;
using Longbow.Cache;
using Longbow.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Bootstrap.DataAccess.Model;
using Longbow.Web.Mvc;
using PetaPoco;
using System.Net;
using System.Text;
using Longbow.Configuration;
using System.Threading;

namespace Bootstrap.DataAccess
{
    /// <summary>
    /// 座席状态监控
    /// </summary>
    public static class AgentStatusHelper
    {
        public const string RetrieveAgentStatusDataKey = "UserHelper-RetrieveAgentStatus";
        public const string RetrieveAgentStatusByRoleIdDataKey = "UserHelper-RetrieveAgentStatusByRoleId";
        public const string RetrieveAgentStatusByGroupIdDataKey = "UserHelper-RetrieveAgentStatusByGroupId";
        public const string RetrieveNewAgentStatusDataKey = "UserHelper-RetrieveNewAgentStatus";
        public const string RetrieveAgentStatusByNameDataKey = "BootstrapUser-RetrieveAgentStatusByName";
        private static readonly object agents = new object();
        /// <summary>
        /// 座席状态监控
        /// </summary>
        /// <returns></returns>
        public static string RetrievePages(PaginationOption op)
        {
            string _AgentStatusUrl = ConfigurationManager.AppSettings["AgentStatusUrl"]; //座席状态监控URL

            string _result = string.Empty;
            postSend(_AgentStatusUrl, "/ALL", out _result);
            if (_result == "[]" || _result == "")
            {
                return "{\"total\":0,\"rows\":[]}";
            }
            return _result;//返回坐席状态json信息


        }


        /// <summary>
        /// 调用wcf
        /// </summary>
        /// <param name="postUrl"></param>
        /// <param name="postData"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool postSend(string postUrl, string postData, out string result)
        {
            try
            {

                string contentType = "application/x-www-form-urlencoded";
                //byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);

                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(postUrl + postData);
                httpWebRequest.Method = "get";
                httpWebRequest.ContentType = contentType;
                //httpWebRequest.ContentLength = byteArray.Length;

                httpWebRequest.Timeout = 60000;

                using (HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse)
                {

                    System.IO.Stream responseStream = httpWebResponse.GetResponseStream();

                    System.IO.StreamReader streamReader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                    result = streamReader.ReadToEnd(); //请求返回的数据
                    result = result.Replace("\\", "").TrimStart('"').TrimEnd('"');
                    httpWebResponse.Close();
                    streamReader.Close();
                    streamReader.Dispose();


                }

                return true;
            }

            catch (Exception ex)
            {
                result = ex.Message;
                return false;
            }
        }
    }
}
